//
//  WBMainViewController.swift
//  小贝微博
//
//  Created by 汪波 on 2017/4/12.
//  Copyright © 2017年 Kevin. All rights reserved.
//

import UIKit
//主控制器
class WBMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildController()
        setupComposeButton()
        
    }
    /**
     portrait:竖屏(肖像)
     landscape:横屏(风景画)
     
        - 使用代码设置接口设备方向,好处,可以在需要横屏的时候,单独处理!!
        - 设置支持的方向之后,当前的控制器及子控制器都会遵守这个方向!
        - 如果播放视频,通常是通过modal展现的!
     */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    // MARK: - 监听方法
    ///撰写微博 
    // FIXME: 没有实现
    //private能够保证方法私有,仅在当前对象被访问
    //@objc允许这个函数在'运行时'通过OC的消息机制被调用!即便是私有了.在运行时也能够访问
    //既能保证安全,又能保证通过target调用到
    @objc fileprivate func composeStatus() {
        print("撰写微博")
        //测试方向旋转
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.cz_random()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
    
    // MARK: - 私有控件
    ///撰写按钮
    fileprivate lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
}
//extension,类似于OC中的分类,在Swift中还可以用来切分代码块
//可以把相近功能的函数,放在一个extension中
//便于代码维护
//注意:和OC的分类一样,extension中不能定义属性,只能定义方法
// MARK: - 设置界面
extension WBMainViewController {
    //设置撰写按钮
    fileprivate func setupComposeButton(){
        tabBar.addSubview(composeButton)
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        //将向内缩进的宽度减小,能让按钮的宽度变大,盖住容错点,防止穿帮
        let w = tabBar.bounds.width / count - 1
        //同OC中的CGRectInset ,正数是向内缩进,负数是向外扩展
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        print("撰写按钮宽度 \(composeButton.bounds.width)")
        
        //按钮监听方法
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    ///设置所有子控制器
    //private仅在本类中访问,fileprivate在当前文件内所有类访问
    fileprivate func setupChildController() {
        let array = [
            ["clsName":"WBHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"],
            //偷懒方法,中间添加一个空的
            ["clsName":"UIViewController"],
            ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"],["clsName":"WBProfileViewController","title":"我","imageName":"profile"]
        ]
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
    }
    
    
    /// 使用字典创建一个子控制器
    ///
    /// - Parameter dict: 信息字典[clsName,title,imageName]
    /// - Returns: 子控制器
    private func controller(dict:[String:String]) -> UIViewController {
        //1.取得字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                return UIViewController()
        }
        //2.创建视图控制器
        //1>将clsName转换成cls
        let vc = cls.init()
        
        vc.title = title
        
        //3.设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //4.设置tabbar的标题字体(有些公司会改大小<注意系统默认是12>)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        //系统默认字体大小是12,修改字体大小,要设置normal的字体大小,设置.highlighted无效
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 16)], for: .normal)
        
        //实例化导航控制器的时候,会调用push方法将rootVC压栈
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
        
    }
}
